{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c064d18d",
   "metadata": {},
   "source": [
    "# 获取token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "dd626a4a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.ed4f362abcb622e862e1695845cc9eb1.315360000.1972727469.282335-26451109', 'expires_in': 2592000, 'session_key': '9mzdXvX1z2yjQUnvnLaeO/jrEfpXeaOLH13u9VSnNYThNNr+2wfV1RnMOi3ugOeCaRMpnjtV1G6j53VmwTx/dswnG8Tz2w==', 'access_token': '24.e0996f87aa18946de066190c5a26078d.2592000.1659959469.282335-26451109', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': '40d2ef90fd9d04e4caba8a0cfe51216b'}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'jk0s4cAURCLLu8iF4M7ebEL9',\n",
    "    'client_secret':'ljIjOPuezr82RA4es9frYGjRhxzs9srk'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "d879ab1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "heeymi_AT = response.json()['access_token']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e4d5693",
   "metadata": {},
   "source": [
    "# 菜品识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "16e73592",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'name': '鱼香肉丝', 'calorie': '154', 'probability': '0.918846', 'baike_info': {}, 'has_calorie': True}, {'name': '肉丝饭', 'calorie': '268', 'probability': '0.0472186', 'baike_info': {}, 'has_calorie': True}, {'name': '酱肉丝', 'calorie': '240', 'probability': '0.013181', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E9%85%B1%E8%82%89%E4%B8%9D/2689787', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/b90e7bec54e736d1fb1014b39b504fc2d562690e', 'description': '酱肉丝是一道传统的家常菜，主要制作食材是猪里脊肉，配料是豆腐皮等，调料是甜面酱、白糖、料酒等，通过大火烧制而成。'}, 'has_calorie': True}, {'name': '肉丝', 'calorie': '174', 'probability': '0.00477472', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E8%82%89%E4%B8%9D/5216182', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/562c11dfa9ec8a13ab7a3a71fd03918fa0ecc03b', 'description': '肉丝即为切成丝状的肉，主要是指猪肉肉丝。肉丝菜主要有青椒肉丝、春笋肉丝、肉丝芝麻、龙爪肉丝、茭白肉丝、酱爆肉丝、钱江肉丝等。'}, 'has_calorie': True}, {'name': '盖浇饭', 'probability': '0.00443594', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E7%9B%96%E6%B5%87%E9%A5%AD/1192126', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/3c6d55fbb2fb43166d2207bff6ec512309f791527597', 'description': '盖浇饭是一种古老的汉族小吃。属于西北甘肃地区叫法，东北叫盖饭，广东叫碟头饭，还有的地方只是叫咖喱土豆饭，咖喱鸡饭，麻婆豆腐饭，以此类推这样子，其实都是菜和饭放在一个盘子里吃，菜浇在饭上，由此称作盖浇饭。其特点饭菜结合、快而热。而风俗各别，如今的盖浇饭，各地作法也不一样，用料也不同，所以也各有特色。'}, 'has_calorie': False}], 'log_id': 1545737411759202242}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "菜品识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/dish\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('C:/Users/Lenovo/Desktop/鱼香肉丝.jpeg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "access_token = heeymi_AT\n",
    "payload={\n",
    "    'access_token':heeymi_AT,\n",
    "    'image':img,\n",
    "    'baike_num':5,\n",
    "    \"top_num\":5\n",
    "}\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c9e8f940",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'name': '水煮鱼', 'calorie': '153', 'probability': '0.917775', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E6%B0%B4%E7%85%AE%E9%B1%BC/30972', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/11385343fbf2b211931330b301ca72380cd7902320b1', 'description': '水煮鱼又称江水煮江鱼、水煮鱼片，是中国川渝地区的一道特色名菜，属于川菜系，其最早流行于重庆市渝北区翠云乡。水煮鱼通常由新鲜草鱼、豆芽、辣椒等食材制作而成。“油而不腻、辣而不燥、麻而不苦、肉质滑嫩”是其特色。'}, 'has_calorie': True}, {'name': '招牌沸腾鱼', 'calorie': '382', 'probability': '0.0178255', 'baike_info': {}, 'has_calorie': True}, {'name': '麻辣鱼', 'calorie': '174', 'probability': '0.00923393', 'baike_info': {}, 'has_calorie': True}, {'name': '酸菜鱼', 'calorie': '98', 'probability': '0.00805494', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E9%85%B8%E8%8F%9C%E9%B1%BC/288202', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/359b033b5bb5c9eaee08644adf39b6003bf3b3a4', 'description': '酸菜鱼也称为酸汤鱼，是一道源自重庆市的经典菜品，以其特有的调味和独特的烹调技法而著称。流行于上世纪90年代，是重庆江湖菜的开路先锋之一。酸菜鱼以草鱼为主料，配以泡菜等食材煮制而成，口味酸辣可口；鱼含丰富优质蛋白，能提供人丰富的蛋白质、矿物质等营养；酸菜中的乳酸可以促进人体对铁元素的吸收，还可以增加人的食欲。关于酸菜鱼的历史来源众说纷纭，也无法考证，后经传承，制作方法也各有不同，但口味基本一致。'}, 'has_calorie': True}, {'name': '印巷香水鱼', 'calorie': '108', 'probability': '0.00423471', 'baike_info': {}, 'has_calorie': True}], 'log_id': 1545737414694552605}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "菜品识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/dish\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('C:/Users/Lenovo/Desktop/水煮鱼.jpeg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "access_token = heeymi_AT\n",
    "payload={\n",
    "    'access_token':heeymi_AT,\n",
    "    'image':img,\n",
    "    'baike_num':5,\n",
    "    \"top_num\":5\n",
    "}\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08df69a9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
